package shaw.wifi.nohttp;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import shaw.wifi.Constants;
import shaw.wifi.NetworkSettingsTaskManager;
import shaw.wifi.network.AsyncNetworkTask;
import shaw.wifi.network.ConnectionInformation;
import android.util.Log;

public abstract class NetworkSettingsNoHttpTask extends AsyncNetworkTask {
	protected SWEncrypt encryption;
	
	public void setEncryptionObject(SWEncrypt crypt) {
		encryption = crypt;
	}
	
	//the socket for communication with the server, and an object stream to send ShawWifiMessages across
	private Socket socket;
	protected ObjectInputStream ois;
	protected ObjectOutputStream oos;
	protected boolean setupSocketConnection() {
		ConnectionInformation connInfo = NetworkSettingsTaskManager.get().getConnInfo();
		if(connInfo.socket != null) {
			socket = connInfo.socket;
			ois = connInfo.ois;
			oos = connInfo.oos;
			
			return true;
		} else {
			try {
				publishProgress("Opening connection to server (" + Constants._HOSTIP + ":" + Constants._SECURITYSERVERPORT + ")...");
				//connect and get streams
				connInfo.socket = socket = new Socket(Constants._HOSTIP, Constants._SECURITYSERVERPORT);
				connInfo.ois = ois = new ObjectInputStream(socket.getInputStream());
				connInfo.oos = oos = new ObjectOutputStream(socket.getOutputStream());
				
				//was successful
				return true;
				
			} catch (UnknownHostException e) {
				publishProgress("Cannot connect to server...");
				e.printStackTrace();
				return false;
			} catch (IOException e) {
				publishProgress("Cannot get communication channel...");
				e.printStackTrace();
				return false;
			}
		}
	}
	protected boolean sendMessage(ShawWifiMessage msg) {
		//send a a message to the stream and send that message to the server
		try {
			msg.sendMessage(oos);
			oos.flush();
			return true;
		} catch (IOException e) {
			publishProgress("Cannot send message to server...");
			e.printStackTrace();
			return false;
		}
	}
	
	protected boolean sendCryptMessage(ShawWifiMessage msg) {
		try {
			msg.sendEncryptedMessage(oos, encryption);
			oos.flush();
			return true;
		} catch (IOException e) {
			publishProgress("Cannot send message to server...");
			e.printStackTrace();
			return false;
		}
		
	}
	
	protected ShawWifiMessage waitForMessage() {
		//we want to wait for a message, so we block until we can read something
		try {
			Log.e("MSG" , "WAITING FOR CRYPT MSG...");
			ShawWifiMessage msg = ShawWifiMessage.readMessage(ois);
			return msg;
		} catch (IOException e) {
			publishProgress("Cannot read data from server...");
			e.printStackTrace();
		}
		
		return null;
	}
	
	protected ShawWifiMessage waitForCryptMessage() {
		//we want to wait for a message, so we block until we can read something
		Log.e("MSG" , "WAITING FOR CRYPT MSG...");
		try {
			ShawWifiMessage msg = ShawWifiMessage.readEncryptedMessage(ois, encryption);
			return msg;
		} catch (IOException e) {
			publishProgress("Cannot read data from server...");
			e.printStackTrace();
		}
		
		return null;
	}
}
